package com.example.springbootbabyu.controller;

import com.example.springbootbabyu.entity.SubCategoryQueryDTO;
import com.example.springbootbabyu.utils.ResultUtil;
import com.example.springbootbabyu.entity.CategoryResult;
import com.example.springbootbabyu.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 功能
 * 作者：梦天
 * 日期：2025/4/17 13:02
 */
@RestController
@RequestMapping("/category")
@CrossOrigin
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/sub")
    public ResultUtil<CategoryResult> getSubCategories(@RequestParam(value = "id", required = false) String id) {
        try {
            log.info("获取子分类，分类ID：{}", id);
            return ResultUtil.success(1, "操作成功", categoryService.getSubCategories(id));
        } catch (Exception e) {
            log.error("获取子分类失败", e);
            return ResultUtil.error(500, "获取子分类失败");
        }
    }
    @GetMapping("/sub/filter")
    public ResultUtil getCategoryFilter(@RequestParam("id") String id) {
        try {
            log.info("获取分类筛选条件，分类ID：{}", id);
            return ResultUtil.success(1, "操作成功", categoryService.getCategoryFilter(id));
        } catch (Exception e) {
            log.error("获取分类筛选条件失败", e);
            return ResultUtil.error(500, "获取分类筛选条件失败");
        }
    }
    @PostMapping("/goods/temporary")
    public ResultUtil getSubCategoryGoods(
            @Valid @RequestBody SubCategoryQueryDTO queryDTO) {
        try {
            log.info("获取子分类商品, queryDTO: {}" , queryDTO);
            return ResultUtil.success(1, "操作成功", categoryService.getSubCategoryGoods(queryDTO));
        } catch (Exception e) {
            log.error("获取子分类商品失败", e);
            return ResultUtil.error(500, "获取子分类商品失败");
        }
    }
}
